/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            HALDebouncer.c
* version:         1.1
* created_by:      Lizette Acosta/Rodrigo Casados
* date_created:    June 22 2013
*=============================================================================*/
/* DESCRIPTION : Debouncer and Button Event Validation                        */
/*============================================================================*/
/* FUNCTION COMMENT : main function of Window Lifter                          */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.1      | 22/June/2013 |                              | Lizette Acosta   */
/*                                                           Rodrigo Casados  */
/*============================================================================*/
#include "stdtypedef.h"
#include "HALDebouncer.h"


uint16_t  Debouncer_Checkbtn(void)
{
uint16_t Btnck=0;
uint16_t Btnck2=0;
static uint16_t Btnval=0;

		if (BTN_1==FALSE)
		{
			 AppFlag2=FALSE;
			 Btnck=Debouncer_Delay(BTN_11);
			 if (Btnck==AUTO)
			 {
			 	Btnval=AUP;
			  }
			  else if (Btnck==NORMAL)
			  {
			 	Btnval=UP;
			  }
			  else
			  {
			  }
		}
		else if(BTN_2==FALSE)
		{
			AppFlag2=FALSE;
			Btnck2=Debouncer_Delay(BTN_22);
			if (Btnck2==AUTO)
			 {
			 	Btnval=ADOWN;
			  }
			  else if (Btnck2==NORMAL)
			  {
			 	Btnval=DOWN;
			  }
			  else
			  {
			  }
		}
		else
		{
		}

	return  Btnval;
}


uint16_t Debouncer_AntiPinch(void)
{
uint16_t count=0;
	while(1)
	    {
	        if (BTN_PINCH==FALSE)// read the value on the switch
	        {                       
	            count++;                // if switch is pressed, increment count
	            if (count > MIN_COUNT)  // if MAX_COUNT is reached...
	            {
	                return PINCH;          // return true and stop reading switch
	            }
	        }
	        else
	        {
		            if (count == FALSE)  
		            {
		                return FALSE;           // return faulse and stop reading switch
		            }
		            else
		            {
		                count--;                // if switch is not pressed, decrement count
		            }
	        }
	    }
}

uint16_t Debouncer_Delay(uint16_t bttn)
{
uint16_t count=0;
uint16_t flag=1;
uint16_t flagt=0;

	while(1)
	    {
	    	flagt=BTN(bttn);
	        if (BTN(bttn)==FALSE)// read the value on the switch
	        {                       
	            count++;                // if switch is pressed, increment count
	            if (count > MIN_COUNT)
	            {
	            	  flag=FALSE;	// if MIN_COUNT is reached activate the flag	
		            if (count > MAX_COUNT)  // if MAX_COUNT is reached...
		            {
		                return TRUE;           // return true and stop reading switch
		            }
	            }
	        }
	        else
	        {
	           if (flag==FALSE)
	           {
	           	return AUTO;
	           }
	           else
	           {
	           	 return FALSE;          
	           }
	        }
	    }
}